import api from './api';
import _ from 'lodash';

const namespace = 'hello';
const resolve = mn => `${namespace}/${mn}`;
export const moduleName = namespace;
export const GETTER_DATA = resolve('getterMsg');
export const SET_DATA = resolve('setMsg');
export const GET = resolve('get');

export const module = function (api) {
  return {
    state: {
      data: []
    },
    getters: {
      [GETTER_DATA]: state => _.cloneDeep(state.data)
    },
    mutations: {
      [SET_DATA](state, data) {
        state.data = data;
      }
    },
    actions: {
      async [GET]({ commit }) {
        const res = await api.get();
        res.success && commit(SET_DATA, res.data);
        return res;
      }
    }
  };
};

export default module(api);
